package com.rockcent.common.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by oyhk on 14-6-29.
 * <p>
 * 客户端工具类
 */
public class RequestUtils extends ServletRequestUtils {

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    /**
     * 获取cookie 的 value
     *
     * @param name
     * @param request
     * @return
     */
    public static String getCookie(String name, HttpServletRequest request) {
        String value = null;
        Cookie[] cookie = request.getCookies();
        if (cookie == null) {
            return value;
        }
        for (int i = 0; i < cookie.length; i++) {
            if (cookie[i].getName().equals(name)) {
                value = cookie[i].getValue();
                if (StringUtils.isBlank(value)) {
                    continue;
                }
                break;
            }
        }
        return value;
    }

    public static String getDomain(HttpServletRequest request) {
        String domain = request.getHeader("domain");
        if (StringUtils.isBlank(domain)) {
            return domain;
        }

        return domain.replace(".local.", ".dev.");
    }

    /**
     * 获取一级域名
     *
     * @param url
     * @return
     */
    public static String getFirstDomain(String url) {
        String domain = null;
        try {
            Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
            Matcher matcher = p.matcher(url);
            matcher.find();
            domain = matcher.group();
        } catch (Exception e) {
        }
        return domain;
    }

    /**
     * 字符串转换成list
     * @param param
     * @return
     */
    public static List<Long> string2list(ObjectMapper objectMapper, String param) {
        if (StringUtils.isBlank(param)) {
            return null;
        }
        List<Long> resultIds = new ArrayList<>();
        try {
            resultIds = objectMapper.readValue(param, new TypeReference<List<Long>>() {
            });
        } catch (IOException e) {
        }
        return resultIds;
    }
}
